package com.example.lambda.test4;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author readpage
 * @create 2022-10-22 13:12
 * 一、构造器引用
 *      和方法引用类型，函数式接口的抽象方法的形参列表和构造器的形参列表一致。
 *      抽象方法的返回值类型即为构造器所属的类的类型
 *
 * 二、数组引用
 *
 */
public class ConstructorRefTest {
    //构造器引用
    //Supplier中的T get()
    //Employee的空参构造器: Employee()
    @Test
    public void test() {
        Supplier<Employee> sup = () -> new Employee();
        System.out.println(sup.get());

        System.out.println("*************");

        Supplier<Employee> sup2 = Employee::new;
        System.out.println(sup2.get());
    }

    //Function中的R apply(T t)
    @Test
    public void test2() {
        Function<Integer, Employee> func = integer -> new Employee(integer);
        Employee employee = func.apply(1001);
        System.out.println(employee);
        System.out.println("************");

        Function<Integer, Employee> func2 = Employee::new;
        Employee employee2 = func2.apply(1002);
        System.out.println(employee2);
    }

    //BiFunction中的R apply(T t, U u)
    @Test
    public void test3() {
        BiFunction<Integer, String, Employee> func = (id, name) -> new Employee(id, name);
        System.out.println(func.apply(1001, "Tom"));
        System.out.println("*************");
        BiFunction<Integer, String, Employee> func2 = Employee::new;
        System.out.println(func2.apply(1002, "Tom"));
    }

    //数组引用
    //Function中的R apply(T t)
    @Test
    public void test4() {
        Function<Integer, String[]> func = length -> new String[length];
        String[] arr = func.apply(5);
        System.out.println(Arrays.toString(arr));
        System.out.println("************");
        Function<Integer, String[]> func2 = String[]::new;
        String[] arr2 = func2.apply(10);
        System.out.println(Arrays.toString(arr2));
    }
}
